// miniprogram/pages/applications/orderdetail/orderdetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let id = options.id;
    this.setData({
      oid: id
    })
    this.getOrderDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  getOrderDetail() {
    let _this = this;
    let id = this.data.oid;
    let db = wx.cloud.database();
    db.collection('tb_order_list').doc(id).get({
      success: res => {
        console.log(res)
        _this.setData({
          oinfo: res.data
        })
      },
      fail: err => {
        console.log
      }
    })
  },
  //取消订单
  cancelOrder(e) {
    console.log(e);
    let db = wx.cloud.database();
    let id = e.currentTarget.dataset.id;
    let _this = this;
    wx.cloud.callFunction({
      name: 'updateOrderStatus',
      data: {
        status: 5,
        ope: 'update',
        id: id
      },
      success: res => {
        console.log(res)
        if (res.result.stats.updated) {
          wx.showToast({
            title: '取消订单成功',
          })
          _this.getOrderDetail();
        } else {
          wx.showToast({
            title: '取消订单失败',
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //立即付款
  confirmPayment(e) {
    let db = wx.cloud.database();
    let id = e.currentTarget.dataset.id;
    let _this = this;
    wx.cloud.callFunction({
      name: 'updateOrderStatus',
      data: {
        status: 2,
        ope: 'update',
        id: id
      },
      success: res => {
        console.log(res)
        if (res.result.stats.updated) {
          wx.showToast({
            title: '取消订单成功',
          })
          _this.getOrderDetail();
        } else {
          wx.showToast({
            title: '取消订单失败',
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //申请退款
  applyRefound(e) {
    let db = wx.cloud.database();
    let id = e.currentTarget.dataset.id;
    let _this = this;
    wx.cloud.callFunction({
      name: 'updateOrderStatus',
      data: {
        status: 6,
        ope: 'update',
        id: id
      },
      success: res => {
        console.log(res)
        if (res.result.stats.updated) {
          wx.showToast({
            title: '退款申请成功',
          })
          _this.getOrderDetail();
        } else {
          wx.showToast({
            title: '退款申请失败',
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //确认收货
  confirmReceive(e) {
    let db = wx.cloud.database();
    let id = e.currentTarget.dataset.id;
    let _this = this;
    wx.cloud.callFunction({
      name: 'updateOrderStatus',
      data: {
        status: 4,
        ope: 'update',
        id: id
      },
      success: res => {
        console.log(res)
        if (res.result.stats.updated) {
          wx.showToast({
            title: '确认收货成功',
          })
          _this.getOrderDetail();
        } else {
          wx.showToast({
            title: '确认收货失败',
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //评论订单
  applyRate(e) {
     let mid=e.currentTarget.dataset.mid;
     wx.navigateTo({
       url: '../orderrate/orderrate?mid='+mid,
     })
  },
  //删除订单
  deleteOrder(e) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})